/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.JavaBean.User;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class UserDAO extends BasicDAO<User> {
	/**
	 * LoginDAO无参构造 重载父类构造器
	 */
	public UserDAO() {
		super();
	}

	/**
	 * LoginDAO有参构造 重载父类构造器
	 */
	public UserDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}

	/**
	 * 新增新用户，并插入到数据库中，参数是User对象
	 */
	public int insertNewUser(User u) {
		int count = 0;
		String sql = "Insert into user values(?,?,?,?,?)";
		try {
			count = super.updateDML(sql, u.getUserId(),u.getPeoId(), u.getPwd(), u.getUserType(), 0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 根据用户名id修改密码
	 */
	public int resetPWDById(String userId, String pwd) {
		int count = 0;
		String sql = "update user set pwd = ? where userid = ?";
		try {
			count = updateDML(sql, pwd, userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * @apiNote 根据用户id删除用户 当数据库内不存在该客户时 返回 0 此时代表已被删除或者根本不存在 若要判断是其中哪一种状态
	 *          请使用get方法并设置excludeDelete为false
	 */
	public int deleteUserById(String userId) {
		int count = 0;
		String sql = "update customer set isDelete = 1 where userid = ?";
		try {
			count = super.updateDML(sql, userId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * getUserInfoById(String uId,boolean excludeDelete) 通过用户Id获取该用户所有信息
	 */
	public User getUserInfoById(String userId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from user where userid = ? ");
		if (excludeDelete)
			sql.append("and isDelete = 0");// 如果排除被删除的，那么只返回未被删除的
		ArrayList<User> u = getInfoFromDB(sql.toString(), userId);
		if (u.size() == 0) {
			return null;
		} else {
			return u.get(0);
		}

	}
	

	@Override
	public ArrayList<User> resultSetToArrayList(ResultSet re) {
		ArrayList<User> user = new ArrayList<User>();

		try {
			while (re.next()) {
				user.add(new User(re.getString("userid"),re.getInt("peoId"), re.getString("pwd"), re.getInt("usertype")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return user;
	}

}
